#include("/front/teacher/_layout.html")
#@layout()
#define layoutImportcss()
#end
#define layoutHead()
<title>发布</title>
#end
#define layoutHome()


<div class="page-content baby-photos-page-content">
	<div class="navbar">
		<div class="navbar-inner toolbstyle">
			<div class="left">
				<a href="javascript:history.back()" class="link back">
					<i class="icon icon-back"></i>
				</a>
			</div>
			<div class="title" id="navbar_title"></div>
			<div class="right">
				<a id="btn_a_save" class="button buttonw font-color-yellow" href="javascript:;">发布</a>
			</div>
		</div>
	</div>
	<form id="my-form">
		
		<input type="hidden" id="picese_id" name="picese_id" value="">
		<input type="hidden" id="picese_type_id" name="picese_type_id" value="">
		<input type="hidden" id="is_baby" name="is_baby" value="">
		
		<div class="block block-babyphotos">
			<div class="block-header">
				<textarea name="picese_info" class="textarea-babyphotos" rows="4" placeholder="说点什么吧..."></textarea>
			</div>
			<div class="baby-photos">
				<div class="baby-photos-item">
					<div class="baby-photos-item-inner">
						<i class="f7-icons">add</i>
						<input type="file" id="fileselector" name="photoselector" multiple>
					</div>
				</div>
			</div>
			<div class="block-footer" id="block_footer_alert">最多添加20张，照片同时上传至相册</div>
			<input type="hidden" id="picese_imgs" name="picese_imgs" value="">
		</div>
		
		<div id="div_tags" class="block block-strong chip-box" style="display:none;">
			<div class="chip-title">选择标签</div>
			<div class="chip">
				<div class="chip-label">作品</div>
			</div>
			<div class="chip">
				<div class="chip-label">重要时刻</div>
			</div>
			<div class="chip">
				<div class="chip-label">生活点滴</div>
			</div>
			<input type="hidden" id="picese_tag" name="picese_tag" value="">
		</div>
		
		<div id="div_students" class="list" style="margin:0; display:none;">
			<ul>
				<li>
					<a class="item-link smart-select smart-select-init" data-open-in="sheet">
						<select id="student_id" name="student_id">
							<option value="">请选择</option>
						</select>
						<div class="item-content">
							<div class="item-inner">
								<div class="item-title" style="color:rgba(0, 0, 0, 0.4);">选择学生</div>
							</div>
						</div>
					</a>
				</li>
			</ul>
		</div>
		<div class="block block-strong share-link" style="display: none;">
			<a href="#">
				<div class="color-yellow">公开</div>
				<i class="f7-icons color-yellow">check</i>
			</a>
		</div>
		<input type="hidden" id="read_power" name="read_power" value="1">
	</form>
</div>
#end
#define layoutImportjs()
#end
#define layoutScript()
<script type="text/javascript">
	var typeid = getUrlParam('typeid');
	var isbaby = getUrlParam('isbaby');
	
	$$('#picese_type_id').val(typeid);
	$$('#is_baby').val(isbaby);
	if (typeid == 2){ $$('#navbar_title').text('发布动态'); }
	else if (isbaby == 1){ $$('#navbar_title').text('宝宝相册');}
	else { $$('#navbar_title').text('班级相册');}
	// 学生相册
	if (isbaby==1){ 
		$$('#div_tags').show(); 
		$$('#div_students').show(); 
		sendPost('/front/teacher/students/getMembers', null, function(res) {
			if (res.code == 200){
				if (isJsonProps(res, 'list_students')){
					var shtml = '';
					for (var i = 0; i < res.list_students.length; i++) {
						var jdata = res.list_students[i];
						shtml += '<option value="'+jdata.student_id+'">'+jdata.student_name+'</option>';
					}
					$$('#student_id').append(shtml);
				}
			}else{
				alert(res.msg);
			}
		});
	}
	
	$$('#student_id').on('change', function(){
		app.smartSelect.close(app.smartSelect.get('.smart-select'));
	});
	
	$$("#fileselector").on("change", function(e) {
		var sval = $$('#picese_imgs').val();
		var arry1 = trim(sval, ';').split(';');
		if (arry1.length >= 20) {
			return false;
		}
		var file = e.target.files[0],
			reader = new FileReader();
		reader.readAsDataURL(file);
		reader.onload = function(ev) {
			if (checkUploadFile(file, 'images')) {
				photo = ev.target.result //获取图片内容：base64字符串
				var formdata = {
					filename: file.name,
					filesize: file.size,
					filetype: '.' + file.type.split("/")[1],
					base64_data: photo
				};

				sendPost('/front/teacher/fileup/upload_base64', formdata, function(res) {
					//console.log(res);
					if (res.code == 0) {
						var html = '<div class="baby-photos-item"><div class="baby-photos-item-inner"><img src="' + getZoomFileUrl(res.fileurl) +
							'"></div></div>';
						$$(".baby-photos").prepend(html);

						sval += res.fileurl + ';';
						$$('#picese_imgs').val(sval);
					} else {
						alert(res.msg);
					}
				});
			}
		};
	})

	var read_power_val = 1;
	$$(".share-link a").on("click", function() {
		$$(this).find("i").toggleClass("color-yellow");
		if (read_power_val == 1){read_power_val=0;}
		else{read_power_val=1;}
		$$('#read_power').val(read_power_val);
	})

	$$(".chip").on("click", function() {
		$$(this).toggleClass("color-orange");
		var tag_name = this.innerText;
		var tag_vals = $$('#picese_tag').val();

		if (this.outerHTML.indexOf('color-orange') > 0) {
			tag_vals += tag_name + ' ';
		} else {
			tag_vals = tag_vals.replace(tag_name + ' ', '');
		}
		$$('#picese_tag').val(tag_vals);
	})

	$$('#btn_a_save').on('click', function() {
		if (validateForm('#my-form')) {
			var formData = app.form.convertToData('#my-form');
			//console.log(JSON.stringify(formData));
			// 调用接口提交数据
			var url = "/front/teacher/lifepieces/save";
			app.request({
				url: url,
				method: "POST",
				dataType: "json", // 返回数据类型，可以是 json
				data: formData,
				success: function(res, status) {
					alert(res.msg);
					if (res.code == 200) {
						window.location.href = '/front/teacher/html/index';
					}
				},
				error: function(xhr, status) {
					alert(status);
				}
			});
		}
		return false;
	});
</script>
#end
